package com.tiny.tinyweb.core.ldap.access;

import java.util.List;

import com.tiny.tinyweb.core.ldap.entry.Person;

public class PersonAccessImpl extends LdapAccess<Person> implements PersonAccess {
    public PersonAccessImpl() {
        super.setManagedClass(Person.class);
    }
    // <editor-fold defaultstate="collapsed" desc="select">
    public Person find(final String cn, final List<String> ous) {
        return super.ldapRead(new Person(cn, ous));
    }

    public Person find(Person entry) {
        return super.ldapRead(entry);
    }

    public List<Person> findPlural(final String base) {
        return super.ldapRead(base, new Person().getObjectClass());
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="insert">
    public void insert(final Person entry) {
        super.ldapCreate(entry);
    }

    public void insertPlural(final List<Person> entries) {
        for (Person entry : entries) {
            this.insert(entry);
        }
    }

    public void deleteInsert(final Person entry) {
        super.ldapDelete(entry);
        super.ldapCreate(entry);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="update">
    public void update(final Person entry, final boolean ignoreNull) {
        super.ldapUpdate(entry, ignoreNull);
    }

    public void update(final Person entry) {
        this.update(entry, true);
    }

    public void updatePlural(final List<Person> entries, final boolean ignoreNull) {
        for (Person entry : entries) {
            this.update(entry, ignoreNull);
        }
    }

    public void updatePlural(final List<Person> entries) {
        this.updatePlural(entries, true);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="delete">
    public void delete(final Person entry) {
        super.ldapDelete(entry);
    }

    public void deletePlural(final List<Person> entries) {
        for (Person entry : entries) {
            this.delete(entry);
        }
    }

    public void deletePlural(final String base) {
        super.ldapDelete(base);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="save">
    public boolean exists(final Person entry) {
        return this.find(entry) != null;
    }

    public void save(final Person entry) {
        if (this.exists(entry)) {
            this.update(entry);
        } else {
            this.insert(entry);
        }
    }

    public void savePlural(List<Person> entries) {
        for (Person entry : entries) {
            this.save(entry);
        }
    }
    // </editor-fold>
}
